package utils
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.geom.Matrix;
	
	public class DisplayUtil
	{
		public function DisplayUtil()
		{
		}
		
		//移除所有子项
		public static function removeAllChild(parent:DisplayObjectContainer,isClearBmp:Boolean = true):void{
			if(parent != null){
				while (parent.numChildren > 0){
					var child:DisplayObject = parent.getChildAt(0);
					if((child is ScaleBitmap == false ) && child is Bitmap && isClearBmp){
						clearBitmapData(child as Bitmap);
					}
					if(child==null)break;
					parent.removeChild(child);
				}
			}
		}
		
		public static function clearBitmapData(bitmap:Bitmap):void{
			if(bitmap!= null && bitmap.bitmapData != null){
				bitmap.bitmapData.dispose();
				bitmap.bitmapData = null;
			}
		}
		
		//横向复制bitmap
		public static function repeatImg(source:BitmapData,width:int,height:int):BitmapData{
			var mySprite:Sprite = new Sprite();
			mySprite.graphics.beginBitmapFill(source,  new Matrix(), true);
			mySprite.graphics.drawRect(0, 0, width, height);
			mySprite.graphics.endFill();
			var copy:BitmapData = new BitmapData(width,height,true,0x00000000);
			copy.draw(mySprite);
			return copy;
		}
		
		public static function remove(target:DisplayObject):void{
			if(target != null && target.parent != null){
				target.parent.removeChild(target);
			}
		}
	}
}